package com.xyz.eduservice.controller;


import com.xyz.entity.R;
import com.xyz.eduservice.entity.EduChapter;
import com.xyz.eduservice.entity.vo.ChapterVo;
import com.xyz.eduservice.service.EduChapterService;


import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-06-11
 */
@Schema(description="课程章节接口")
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @Schema(description="嵌套章节数据列表")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(
            @Schema(name = "courseId", description = "课程ID", required = true)
            @PathVariable String courseId){
        List<ChapterVo> list= eduChapterService.getChapterVideoByCourseId(courseId);

        return R.ok().data("items",list);
    }

    //章节下有小节不可以删除
    //TODO  可能会做功能 删除章节下的所有小节
    @Schema(description="根据ID删除章节")
    @DeleteMapping("{id}")
    public R removeById(
            @Schema(name = "id", description = "章节ID", required = true)
            @PathVariable String id){

        boolean result = eduChapterService.removeChapterById(id);

        return result ? R.ok():R.error().message("删除失败");
    }

    @Schema(description="新增章节")
    @PostMapping("saveChapter")
    public R saveChapter(
            @Schema(name = "chapterVo", description = "章节对象", required = true)
            @RequestBody EduChapter chapter){

        eduChapterService.save(chapter);
        return R.ok();
    }

    @Schema(description="根据ID查询章节")
    @GetMapping("{id}")
    public R getChapterById(
            @Schema(name = "id", description = "章节ID", required = true)
            @PathVariable String id){

        EduChapter chapter = eduChapterService.getById(id);
        return R.ok().data("item", chapter);
    }

    @Schema(description="根据实体修改章节")
    @PostMapping("upChapterDate")
    public R upChapterDate(
            @Schema(name = "chapter", description = "章节对象", required = true)
            @RequestBody EduChapter chapter){

        eduChapterService.updateById(chapter);
        return R.ok();
    }


}

